let target;
let operation;
let userId_const = localStorage.getItem("userId");
let userName_const = localStorage.getItem("userName");
let username = $(".dropdown-user-name");
username.find('i').remove();
let user_name = '<i>你好，'+userName_const+'老师</i>';
username.append(user_name);                       //生成导航栏的老师名字
username = $("#user_name");
username.find('span').remove();
user_name = '<span>'+userName_const+'老师</span>'      //生成导航栏下拉框的老师名字
username.append(user_name);

let examId_arr=[];
let examName_arr=[];

function init(){
    $.ajax({
        url: '/method/getAllJson',
        type: 'get',
        dataType: 'json',
        contentType: 'application/json',
        async:false,
        data:{"userId":userId_const},
        success: function (data) {
            $("#exam_point").find('tr').remove();
            examId_arr=[];
            examName_arr=[];
            let all_evaluation_way;//所有的评价方式
            let tr;
            for (let i = 0; i < data.length; i++) {

                $.ajax({
                    url: '/lv_course/findNameById',
                    type: 'get',
                    dataType: 'json',
                    async:false,
                    data:{"courseId":data[i].courseId},
                    success: function (data) {
                        courseName=data.courseName;
                        examName_arr.push(courseName);
                    }
                });
                if(data[i].evaluaKeywords)
                all_evaluation_way = data[i].evaluaKeywords.split(',');
                else all_evaluation_way=null;
                for (let j in all_evaluation_way) {
                    if (all_evaluation_way[j] === "期末考试") {
                        examId_arr.push(i+1);
                        findEvaluationWayById(i);
                        tr = '<th scope="row">' + data[i].courseId + '</th>' + '<td>' + courseName + '</td>' + '<td>' + target + '</td>' + '<td><a href="#"> ' + operation + '</a></td>';
                        $("#exam_point").append('<tr>' + tr + '</tr>');
                    }
                }
                localStorage.setItem('examId',examId_arr);
                localStorage.setItem('examName',examName_arr);
                passParam2URL();
            }
        }
    })
}
function findEvaluationWayById(courseId){
    $.ajax({
        url: '/target/findEvaluationWayById',
        type: 'get',
        dataType: 'json',
        async:false,
        data: {"courseId": courseId+1},
        success: function (data) {
            for (let i in data) {
                if (data[i].examPoint) {
                    target = '是';
                    operation = '修改';
                } else {
                    target = '否';
                    operation = '填写';
                }

            }
        }
    });
}


//通过url传递参数
function passParam2URL(){
    $("#exam_point tr").find('a').click(function(){
        let id = $(this).parents("tr").find('th').text();
        window.open('./exam-point-input.html?courseId='+id,'_self');
    })
}

//给登录按钮绑定回车键
$("body").keydown(function () {
    if (event.keyCode == "13"){    //13是enter键的键码
        $("#btn-find-exam-point").click();
    }
});


$(document).ready(function(){
    init();
    $("#btn-find-exam-point").click(function (){
        let inputText = $("#find-exam-point").val();
        let reg = new RegExp("^[0-9]*[1-9][0-9]*$");
        if(inputText.match(/^[ ]*$/)) {
            init();
        }
        if(inputText.match(reg)) {
            let flag=false;
            for(let i = 0;i<examId_arr.length;i++){
                if(inputText==examId_arr[i]){
                    $.ajax({
                        url:'/method/findById',
                        type:'get',
                        dataType: 'json',
                        contentType:'application/json',
                        data:{"courseId":inputText},
                        success:function (data) {

                            $.ajax({
                                url: '/lv_course/findNameById',
                                type: 'get',
                                dataType: 'json',
                                async:false,
                                data:{"courseId":inputText},
                                success: function (data) {
                                    courseName=data.courseName;
                                }
                            });
                            let all_evaluation_way;//所有的评价方式
                            let tr;
                            if (data == null)
                                swal({title:"未找到相关课程",type:"warning"});
                            else {
                                $("#exam_point").find('tr').remove();
                                if(data.evaluaKeywords)
                                all_evaluation_way = data.evaluaKeywords.split(',');
                                else all_evaluation_way=null;
                                for (let i in all_evaluation_way) {
                                    if (all_evaluation_way[i] === "期末考试") {
                                        findEvaluationWayById(parseInt(inputText)-1);
                                        tr = '<th scope="row">' + data.courseId + '</th>' + '<td>' + courseName + '</td>' + '<td>' + target + '</td>' + '<td><a href="#">' + operation + '</a></td>'
                                        $("#exam_point").append('<tr>' + tr + '</tr>');
                                    }
                                }
                            }
                            passParam2URL();
                        }
                    });
                    flag=true;
                    break;
                }
            }
            if(flag==false)
                swal({title:"未找到相关课程",type:"warning"});
        }else{
            let flag=false;
            $("#exam_point").find('tr').remove();
            for(let i = 0;i<examName_arr.length;i++) {
                if (examName_arr[i].indexOf(inputText) >= 0) {
                    console.log(examName_arr[i]+":"+examName_arr[i].indexOf(inputText));
                    $.ajax({
                        url:'/method/findById',
                        type:'get',
                        dataType: 'json',
                        contentType:'application/json',
                        data:{"courseId":examId_arr[i]},
                        success:function (data){
                            let all_evaluation_way;//所有的评价方式
                            let tr;

                                    $.ajax({
                                        url: '/lv_course/findNameById',
                                        type: 'get',
                                        dataType: 'json',
                                        async:false,
                                        data:{"courseId":data.courseId},
                                        success: function (data) {
                                            courseName=data.courseName;

                                        }
                                    });
                                    if (data.evaluaKeywords)
                                    all_evaluation_way = data.evaluaKeywords.split(',');
                                    else all_evaluation_way=null;
                                    console.log(all_evaluation_way);
                                    for (let j in all_evaluation_way) {
                                        if (all_evaluation_way[j] === "期末考试") {
                                            findEvaluationWayById(data.courseId-1);
                                            tr = '<th scope="row">' + data.courseId + '</th>' + '<td>' + courseName + '</td>' + '<td>' + target + '</td>' + '<td><a href="#"> ' + operation + '</a></td>';
                                            $("#exam_point").append('<tr>' + tr + '</tr>');
                                        }
                                    }
                                    passParam2URL();
                                }
                    });
                    flag=true;
                }
            }
            if(flag===false)
                swal({title:"未找到相关课程",type:"warning"});
        }
    })
})
